package com.estate.system.service.impl;

import java.util.List;

import com.google.gson.Gson;
import com.estate.common.utils.redis.RedisUtil;
import com.estate.system.domain.SecondHand;
import com.estate.system.domain.SecondHandDetailAndSecondHand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.estate.system.mapper.SecondHandDetailMapper;
import com.estate.system.domain.SecondHandDetail;
import com.estate.system.service.ISecondHandDetailService;
import com.estate.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author estate
 * @date 2022-05-28
 */
@Service
public class SecondHandDetailServiceImpl implements ISecondHandDetailService
{
    @Resource
    private SecondHandDetailMapper secondHandDetailMapper;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SecondHandDetail selectSecondHandDetailById(Long id)
    {

        Gson gson = new Gson();
        String secondHandDetailJson = (String)redisUtil.get("secondHandDetail:" + id);
        //有可能存在缓存穿透的问题
        SecondHandDetail secondHandDetail = new SecondHandDetail();
        if (secondHandDetailJson!=null && secondHandDetailJson.equals("noExist")){
            System.out.println("bothnoExist1");
            return null;
        }else if(secondHandDetailJson==null){
            secondHandDetail=secondHandDetailMapper.selectSecondHandDetailById(id);
            if (secondHandDetail==null){
                redisUtil.set("secondHandDetail:"+id,"noExist");
                System.out.println("bothnoExist");
                return null;
            }else{
                String s = gson.toJson(secondHandDetail);
                redisUtil.set("secondHandDetail:"+id,s);
                return secondHandDetail;
            }

        }else {
            //不等于null和noExist,还有也不是null,说明存在直接返回
            SecondHandDetail secondHandDetail1 = gson.fromJson(secondHandDetailJson, SecondHandDetail.class);

            return secondHandDetail1;

        }




    }

    /**
     * 左连接查出 secondHand和detail表共同的信息
     *  用redis改造过 还未测试
     * **/
    @Override
    public SecondHandDetailAndSecondHand selectSecondHandDetailAndSecondHandList(SecondHandDetail secondHandDetail) {

        return secondHandDetailMapper.selectSecondHandDetailAndSecondHandList(secondHandDetail);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param secondHandDetail 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SecondHandDetail> selectSecondHandDetailList(SecondHandDetail secondHandDetail)
    {
        return secondHandDetailMapper.selectSecondHandDetailList(secondHandDetail);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param secondHandDetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSecondHandDetail(SecondHandDetail secondHandDetail)
    {

        int i = secondHandDetailMapper.insertSecondHandDetail(secondHandDetail);
        if (i>0){
            redisUtil.del("secondHandDetail:"+secondHandDetail.getId());
            return i;
        }
        return i;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param secondHandDetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSecondHandDetail(SecondHandDetail secondHandDetail)
    {
        int i = secondHandDetailMapper.updateSecondHandDetail(secondHandDetail);
        if (i>0){
            redisUtil.del("secondHandDetail:"+secondHandDetail.getId());
            return i;
        }
        return i;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSecondHandDetailByIds(String ids)
    {

        int i = secondHandDetailMapper.deleteSecondHandDetailByIds(Convert.toStrArray(ids));
        if (i>0){
            String[] strings = Convert.toStrArray(ids);
            for (String key:strings){
                redisUtil.del("secondHandDetail:"+key);
            }

            return i;
        }
        return i;
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSecondHandDetailById(Long id)
    {
        int i = secondHandDetailMapper.deleteSecondHandDetailById(id);
        if(i>0){
            redisUtil.del("secondHandDetail:"+id);
            return i;
        }
        return i;
    }
}
